#include <Windows.h>
#include <tchar.h>
#include "resource.h"
#include <Psapi.h>
#include <windowsx.h>
#pragma comment(lib, "Psapi.lib")

#define IDT_UPDATE		0X7000

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0111"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			SetTimer(hDlg, IDT_UPDATE, 1000, NULL);
			break;
		}
		case WM_TIMER:
		{
			PROCESS_MEMORY_COUNTERS pmc = {0};
			GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
			pmc.cb	= sizeof(pmc);
			TCHAR szTmp[MAX_PATH];
			_stprintf(szTmp, _T("max memory: %d  using memory:  %d"), pmc.PeakWorkingSetSize/1024, pmc.WorkingSetSize/1024);
			ListBox_AddString(GetDlgItem(hDlg, IDC_LIST1), szTmp);
			SendMessage(GetDlgItem(hDlg, IDC_LIST1), WM_VSCROLL, SB_BOTTOM, 0);
			break;
		}
		case WM_COMMAND:
		{
			static char* p = NULL;
			switch (LOWORD(wParam))
			{
				case IDC_BTNNEW:
				{
					if (NULL == p)
					{
						p = new char[1024];
					}
					break;
				}
				case IDC_BTNDEL:
				{
					if (NULL != p)
					{
						delete[] p;
						p = NULL;
					}
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PROCESS_MEMORY_COUNTERS pmc = {0};
			GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
			pmc.cb	= sizeof(pmc);
			TCHAR szTmp[MAX_PATH];
			_stprintf(szTmp, _T("%d   %d"), pmc.PeakWorkingSetSize/1024, pmc.WorkingSetSize/1024);
			OutputDebugString(szTmp);

			//Sleep(5000);

			PostQuitMessage(0);



			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}